/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.features;

import java.util.HashMap;
import java.util.Map;
import org.osdtsystem.matrixparser.features.keys.FeatureKey;

/**
 *
 * @author Martin Haulrich
 */
public class MapTwoWayFeatureHandler extends MapFeatureHandler implements TwoWayFeatureHandler {

    Map<Feature, Object> feature2key;

    public MapTwoWayFeatureHandler() {
        super();
        feature2key = new HashMap<Feature, Object>();
        feature2key.put(UNKNOWN, UNKNOWNNAME);
    }

    @Override
    public Feature getFeature(FeatureKey key) {
        Feature f = map.get(key);
        if (f == null) {
            if (!grow) {
                return UNKNOWN;
            }
            f = new Feature(nextNr);
            nextNr++;
            FeatureKey clone = key.copy();
            map.put(clone, f);
            feature2key.put(f, clone);
        }
        return f;
    }

    @Override
    public Feature getFeature(String key) {
        Feature f = map.get(key);
        if (f == null) {
            if (!grow) {
                return UNKNOWN;
            }
            f = new Feature(nextNr);
            nextNr++;
            map.put(key, f);
            feature2key.put(f, key);
        }
        return f;
    }

    public String getName(Feature f) {
        return feature2key.get(f).toString();
    }

    public String getName(int i) {
        Feature f = new Feature(i);
        return getName(f);
    }

    @Override
    public String toString() {
        return feature2key.toString();
    }

    public boolean alphabetGrows() {
        return grow;
    }
}
